//
//  ShowMusicListController.m
//  HB
//
//  Created by 陶秋安 on 16/3/10.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "ShowMusicListController.h"
#import "MusicListCell.h"
#import "MineOperation.h"
#import "ZYMusicTool.h"

@implementation ShowMusicListController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _array=[[NSMutableArray alloc]init];
    
    self.tableMusicList.dataSource=self;
    
    self.tableMusicList.delegate=self;
    
    if([self.pageTitle isEqualToString:@"最近播放"])
    {
        [self loadViewRes];
    }
    else if([self.pageTitle isEqualToString:@"我的收藏"])
    {
        [self loadMyCollection];
    }
    else if([self.pageTitle isEqualToString:@"我的上传"])
    {
        [self loadUploadMusic];
    }
 
    else if([self.pageTitle isEqualToString:@"我的碟包"])
    {
        [self loadMyPakage];
    }
    
}

-(void)setNavigationLeft:(NSString *)imageName sel:(SEL)sel
{
    
}

- (void)setNavigationRight:(NSString *)imageName sel:(SEL)sel
{
    
}

- (void)setNavigationTitleImage:(NSString *)imageName
{
    self.navigationItem.title = self.pageTitle;
}


//加载最近播放
-(void) loadViewRes
{
    _array = [_db getViewResourse];
    
    [self setTableHeight:_array.count];
    
}

-(void) setTableHeight:(NSInteger) count
{
//    CGFloat height= 70.0f;
//    
//    if(count>0 )
//    {
//        if(count<=9)
//            height=height*count;
//        else
//            height=height*9;
//
//    }
//    else
//    {
//        height= self.view.bounds.size.height;
//    }
//    
    //ImageView高度为父视图高度
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.tableMusicList attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant: DeviceHeight-66];
    
    [self.view addConstraint:heightConstraint];
    
}

-(void) setTableHeightSync:(NSInteger) count
{
//    CGFloat height= 70.0f;
//    
//    if(count>0)
//    {
//        height=height*count;
//    }
//    else
//    {
//        height= self.view.bounds.size.height;
//    }
//    
    dispatch_sync(dispatch_get_main_queue(), ^{
        //ImageView高度为父视图高度
        NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.tableMusicList attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:DeviceHeight-66];
        
        [self.view addConstraint:heightConstraint];
//
    });
}


//我的上传
-(void) loadUploadMusic
{
    operation = [[MineOperation alloc] initWithDelegate:self];
    
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"UploadInfo"];
    
    NSDictionary * dictInfo = @{@"url":[NSString stringWithFormat:@"%@?djID=%@",URL,djID]};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:2];
}

//我的收藏
-(void) loadMyCollection
{
    operation = [[MineOperation alloc] initWithDelegate:self];
    
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"CollectionInfo"];
    
    NSDictionary * dictInfo = @{@"url":[NSString stringWithFormat:@"%@?djID=%@",URL,djID]};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:3];
}

//我的碟包
-(void) loadMyPakage
{
    operation = [[MineOperation alloc] initWithDelegate:self];
    
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"CDPackageInfo"];
    
    NSDictionary * dictInfo = @{@"url":[NSString stringWithFormat:@"%@?djID=%@",URL,djID]};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:4];
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation=nil;
    
    _mArray= [[NSMutableArray alloc] init];
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
    {
        NSArray *dataArray=[dataParse objectForKey:@"data"];
        
        for (NSInteger i=0; i<[dataArray count]; i++)
        {
    
            NSDictionary *dict= [dataArray objectAtIndex:i];
            
            NSMutableDictionary *rowDict=[[NSMutableDictionary alloc]init];
            
            [rowDict setObject:[dict objectForKey:@"MusicID"] forKey:@"musicID"];
            
            [rowDict setObject:[dict objectForKey:@"MusicName"] forKey:@"Title"];
            
            [rowDict setObject:[dict objectForKey:@"Icon"] forKey:@"icon"];
            
            [rowDict setObject:[dict objectForKey:@"IconName"] forKey:@"iconName"];
            
            [rowDict setObject:[dict objectForKey:@"musiclist"] forKey:@"musiclist"];
            
            [rowDict setObject:[dict objectForKey:@"singer"] forKey:@"singer"];
            
            [rowDict setObject:[dict objectForKey:@"ID"] forKey:@"djID"];
            
            [_mArray addObject:rowDict];
        }
        
        [self setTableHeightSync:_mArray.count];
        
        [self.tableMusicList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
}

#pragma mark 设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([self.pageTitle isEqualToString:@"最近播放"])
    {
        return _array.count;
    }
    else
    {
        return _mArray.count;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
#pragma mark 表格绑定数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *idefine = @"MusicListCell";
    
    MusicListCell *cell = [tableView dequeueReusableCellWithIdentifier:idefine];
    
    if (cell==nil)
    {
        cell = [[MusicListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idefine];
    }
    NSDictionary *dict;
    if([self.pageTitle isEqualToString:@"最近播放"])
    {
        dict = [_array objectAtIndex:indexPath.row];
    }
    else
    {
        dict = [_mArray objectAtIndex:indexPath.row];
    }
    
    ZYMusic *music=[[ZYMusic alloc] init];
    
    music.musicID=[dict objectForKey:@"musicID"];
    
    music.name=[dict objectForKey:@"Title"];
    
    music.icon=[dict objectForKey:@"icon"];
    
    music.iconName=[dict objectForKey:@"iconName"];
    
    music.musicListArray = [dict objectForKey:@"musiclist"];
    
    music.singer=[dict objectForKey:@"singer"];
    
    music.djID =[dict objectForKey:@"djID"];
    
    music.shareURL=[dict objectForKey:@"shareURL"];
    
    cell.lblMusicTitle.text=music.name;
    
    cell.lblMusicInfo.text=music.singer;
    
    cell.music= music;
    
    [Common downLoadImageByURL:cell.imgMusicIcon andURL:music.icon andName:music.iconName];
    
    return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MusicListCell *cell = (MusicListCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if(![[ZYMusicTool playingMusic].filename isEqualToString:cell.music.name])
    {
        [ZYMusicTool playingMusic].playing=NO;
        
        //获取新的歌曲
        ZYMusic *currMusic= cell.music;
        
        currMusic.playing = YES;
        
        [ZYMusicTool setPlayingMusic:currMusic];
        
        [self addViewRes:currMusic];
        
    }
    [self musicPlay].playMode=@"one";
    
    [self.view.window.rootViewController presentViewController:[self musicPlay] animated:true completion:nil];
    
}
@end
